package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldAssets;
import com.cleaningcloud.cld.domain.CldAssetsDepreciation;
import com.cleaningcloud.cld.domain.CldAssetsDetails;
import com.cleaningcloud.cld.domain.CldAssetsRecord;
import com.cleaningcloud.cld.domain.dto.CldAssetsDepreciationDto;
import com.cleaningcloud.cld.domain.dto.CldAssetsRecordDto;
import com.cleaningcloud.cld.domain.vo.CldAssetsDepreciationVo;
import com.cleaningcloud.cld.domain.vo.CldAssetsRecordVo;
import com.cleaningcloud.cld.mapper.CldAssetsDepreciationMapper;
import com.cleaningcloud.cld.mapper.CldAssetsDetailsMapper;
import com.cleaningcloud.cld.mapper.CldAssetsMapper;
import com.cleaningcloud.cld.mapper.CldAssetsRecordMapper;
import com.cleaningcloud.cld.service.ICldAssetsDepreciationService;
import com.cleaningcloud.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

/**
 * 资产折旧Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-07
 */
@Service
public class CldAssetsDepreciationServiceImpl implements ICldAssetsDepreciationService {
    @Autowired
    private CldAssetsDepreciationMapper cldAssetsDepreciationMapper;

    @Autowired
    private CldAssetsRecordMapper cldAssetsRecordMapper;

    @Autowired
    private CldAssetsMapper cldAssetsMapper;

    @Autowired
    private CldAssetsDetailsMapper assetsDetailsMapper;


    /**
     * 查询资产折旧
     *
     * @param assetsDepreciationId 资产折旧主键
     * @return 资产折旧
     */
    @Override
    public CldAssetsDepreciation selectCldAssetsDepreciationByAssetsDepreciationId(Long assetsDepreciationId) {
        return cldAssetsDepreciationMapper.selectCldAssetsDepreciationByAssetsDepreciationId(assetsDepreciationId);
    }

    /**
     * 查询资产折旧列表
     *
     * @param cldAssetsDepreciation 资产折旧
     * @return 资产折旧
     */
    @Override
    public List<CldAssetsDepreciationVo> selectCldAssetsDepreciationList(CldAssetsDepreciationDto cldAssetsDepreciation) {
        return cldAssetsDepreciationMapper.selectCldAssetsDepreciationList(cldAssetsDepreciation);
    }

    @Override
    public List<CldAssetsDepreciationVo> getCldAssetsDepreciationList(CldAssetsDepreciationDto cldAssetsDepreciation) {
        return cldAssetsDepreciationMapper.getCldAssetsDepreciationList(cldAssetsDepreciation);
    }

    @Override
    public List<CldAssetsDepreciationVo> selectCldAssetsDepreciationGroupProjectList(CldAssetsDepreciationDto cldAssetsDepreciation) {
        return cldAssetsDepreciationMapper.selectCldAssetsDepreciationGroupProjectList(cldAssetsDepreciation);
    }

    /**
     * 新增资产折旧
     *
     * @param cldAssetsDepreciation 资产折旧
     * @return 结果
     */
    @Override
    public int insertCldAssetsDepreciation(CldAssetsDepreciation cldAssetsDepreciation) {
        return cldAssetsDepreciationMapper.insertCldAssetsDepreciation(cldAssetsDepreciation);
    }

    /**
     * 修改资产折旧
     *
     * @param cldAssetsDepreciation 资产折旧
     * @return 结果
     */
    @Override
    public int updateCldAssetsDepreciation(CldAssetsDepreciation cldAssetsDepreciation) {
        return cldAssetsDepreciationMapper.updateCldAssetsDepreciation(cldAssetsDepreciation);
    }

    /**
     * 批量删除资产折旧
     *
     * @param assetsDepreciationIds 需要删除的资产折旧主键
     * @return 结果
     */
    @Override
    public int deleteCldAssetsDepreciationByAssetsDepreciationIds(Long[] assetsDepreciationIds) {
        return cldAssetsDepreciationMapper.deleteCldAssetsDepreciationByAssetsDepreciationIds(assetsDepreciationIds);
    }

    /**
     * 删除资产折旧信息
     *
     * @param assetsDepreciationId 资产折旧主键
     * @return 结果
     */
    @Override
    public int deleteCldAssetsDepreciationByAssetsDepreciationId(Long assetsDepreciationId) {
        return cldAssetsDepreciationMapper.deleteCldAssetsDepreciationByAssetsDepreciationId(assetsDepreciationId);
    }

    @Override
    @Transactional
    public void calculateAssetsDepreciationTask() {
        LocalDate localDate = LocalDate.now();
        // 查询投放中的资产
        CldAssetsRecordDto record = new CldAssetsRecordDto();
        record.setRecordStatus(2L);
        record.setIsNewUpdate(1L);
        List<CldAssetsRecordVo> records = cldAssetsRecordMapper.selectCldAssetsRecordList(record);
        for (CldAssetsRecordVo cldAssetsRecordVo : records) {
            CldAssetsDepreciation cldAssetsDepreciation = new CldAssetsDepreciation();
            cldAssetsDepreciation.setAssetsId(cldAssetsRecordVo.getAssetsId());
            cldAssetsDepreciation.setAssetsIdCard(cldAssetsRecordVo.getAssetsIdCard());
            cldAssetsDepreciation.setYear(localDate.getYear());
            cldAssetsDepreciation.setMonth(localDate.getMonthValue());
            cldAssetsDepreciation.setDay(localDate.getDayOfMonth());
            cldAssetsDepreciation.setProjectId(cldAssetsRecordVo.getProjectId());
            cldAssetsDepreciation.setCreateTime(DateUtils.getNowDate());
            cldAssetsDepreciation.setStatus(2L);
            // 购置单价 / 折旧期数  = 折旧金额 / 当月天数
            BigDecimal deMoney = new BigDecimal(0);
            BigDecimal b2 = new BigDecimal(cldAssetsRecordVo.getAssetsPeriodNumber());
            cldAssetsDepreciation.setMoney(cldAssetsRecordVo.getBulMoney().divide(b2, 2, RoundingMode.HALF_UP).divide(BigDecimal.valueOf(localDate.lengthOfMonth()), RoundingMode.HALF_UP));

            if(null != cldAssetsRecordVo.getIsDepreciation()){
                if(cldAssetsRecordVo.getIsDepreciation() == 2){
                    cldAssetsDepreciation.setLossType(1L);//折旧
                }else{
                    cldAssetsDepreciation.setLossType(2L);//增值
                }
            }
            cldAssetsDepreciationMapper.insertCldAssetsDepreciation(cldAssetsDepreciation);

            //计算折旧是否已过期
            CldAssetsDepreciationDto dto = new CldAssetsDepreciationDto();
            dto.setAssetsIdCard(cldAssetsRecordVo.getAssetsIdCard());
            List<CldAssetsDepreciationVo> list = cldAssetsDepreciationMapper.selectCldAssetsDepreciationList(dto);
            if(!list.isEmpty()){
                CldAssets assets = cldAssetsMapper.selectCldAssetsByAssetsId(cldAssetsRecordVo.getAssetsId());
                if(null != assets){
                    //折旧期数*30天
                    int days = Integer.parseInt(assets.getAssetsPeriodNumber().toString()) * 30;
                    if(list.size() >= days){
                        CldAssetsDetails details = assetsDetailsMapper.selectCldAssetsDetailsByAssetsIdCard(cldAssetsRecordVo.getAssetsIdCard());
                        if(null != details){
                            details.setIsDepreciation(1L);//折旧已过期 1是 2否
                            assetsDetailsMapper.updateCldAssetsDetails(details);
                        }
                    }
                }
            }
        }
    }
}
